import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  NotFoundException,
  Param,
  Post,
  Put,
  ValidationPipe,
} from '@nestjs/common';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Hotel } from '../entities/hotel.entity';
import { Room } from '../entities/room.entity';
import { CreateHotelDto, UpdateHotelDto } from './dto';
import { HotelsService } from './hotels.service';

@ApiTags('hotels')
@Controller('hotels')
export class HotelsController {
  constructor(private readonly hotelsService: HotelsService) {}

  @Get()
  @ApiOperation({ summary: '获取所有酒店', description: '返回所有酒店的列表' })
  @ApiResponse({ status: 200, description: '成功获取酒店列表', type: [Hotel] })
  async findAll(): Promise<Hotel[]> {
    return this.hotelsService.findAll();
  }

  @Get(':id')
  @ApiOperation({ summary: '获取指定酒店', description: '根据ID获取酒店详情' })
  @ApiParam({ name: 'id', description: '酒店ID' })
  @ApiResponse({ status: 200, description: '成功获取酒店详情', type: Hotel })
  @ApiResponse({ status: 404, description: '酒店不存在' })
  async findOne(@Param('id') id: string): Promise<Hotel> {
    const hotelId = parseInt(id, 10);
    if (isNaN(hotelId)) {
      throw new NotFoundException('酒店ID必须是数字');
    }
    return this.hotelsService.findOne(hotelId);
  }

  @Get(':id/rooms')
  @ApiOperation({
    summary: '获取酒店的房间',
    description: '获取指定酒店的所有房间',
  })
  @ApiParam({ name: 'id', description: '酒店ID' })
  @ApiResponse({ status: 200, description: '成功获取房间列表', type: [Room] })
  @ApiResponse({ status: 404, description: '酒店不存在' })
  async findHotelRooms(@Param('id') id: string): Promise<Room[]> {
    const hotelId = parseInt(id, 10);
    if (isNaN(hotelId)) {
      throw new NotFoundException('酒店ID必须是数字');
    }
    return this.hotelsService.findHotelRooms(hotelId);
  }

  @Post()
  @ApiOperation({ summary: '创建酒店', description: '创建新的酒店' })
  @ApiResponse({ status: 201, description: '成功创建酒店', type: Hotel })
  async create(
    @Body(ValidationPipe) createHotelDto: CreateHotelDto,
  ): Promise<Hotel> {
    return this.hotelsService.create(createHotelDto);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新酒店', description: '更新指定ID的酒店信息' })
  @ApiParam({ name: 'id', description: '酒店ID' })
  @ApiResponse({ status: 200, description: '成功更新酒店', type: Hotel })
  @ApiResponse({ status: 404, description: '酒店不存在' })
  async update(
    @Param('id') id: string,
    @Body(ValidationPipe) updateHotelDto: UpdateHotelDto,
  ): Promise<Hotel> {
    const hotelId = parseInt(id, 10);
    if (isNaN(hotelId)) {
      throw new NotFoundException('酒店ID必须是数字');
    }
    return this.hotelsService.update(hotelId, updateHotelDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除酒店', description: '删除指定ID的酒店' })
  @ApiParam({ name: 'id', description: '酒店ID' })
  @ApiResponse({ status: 204, description: '成功删除酒店' })
  @ApiResponse({ status: 404, description: '酒店不存在' })
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Param('id') id: string): Promise<void> {
    const hotelId = parseInt(id, 10);
    if (isNaN(hotelId)) {
      throw new NotFoundException('酒店ID必须是数字');
    }
    return this.hotelsService.remove(hotelId);
  }
}
